//
//  AsynDemoVC.m
//  Demo
//
//  Created by lx on 2018/12/26.
//  Copyright © 2018 mgz. All rights reserved.
//

#import "AsynDemoVC.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import "AsynModel.h"
#import "AsynCell.h"
@interface AsynDemoVC ()<ASTableDelegate,ASTableDataSource>
@property(strong,nonatomic)ASTableNode* tableNode;
@property(strong,nonatomic)NSMutableArray *dataArr;
@end

@implementation AsynDemoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    _tableNode = [[ASTableNode alloc]initWithStyle:UITableViewStylePlain];
    // 2
    self.tableNode.dataSource = self;
    self.tableNode.delegate = self;
    // 3
    self.tableNode.view.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 4
    self.tableNode.view.leadingScreensForBatching = 1.0;
    // 5
    [self.view addSubnode:self.tableNode];
}
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    self.tableNode.frame = CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-CGRectGetHeight(self.navigationController.navigationBar.frame));
}

- (NSInteger)tableNode:(ASTableNode *)tableNode numberOfRowsInSection:(NSInteger)section {
    // 1
    return self.dataArr.count;
}

- (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 2
    AsynModel* model = self.dataArr[indexPath.row];
//
//    // 3
    ASCellNode *(^ASCellNodeBlock)() = ^ASCellNode *() {
        AsynCell *cellNode = [[AsynCell alloc]initWithAsynModel:model];
        return cellNode;
    };
    
    return ASCellNodeBlock;
}
- (NSInteger)numberOfSectionsInTableNode:(ASTableNode *)tableNode{
    // 4
    return 1;
}
// 1
- (BOOL)shouldBatchFetchForTableNode:(ASTableNode *)tableNode {
    return YES;
}
//2
- (void)tableNode:(ASTableNode *)tableNode willBeginBatchFetchWithContext:(ASBatchContext *)context
{
    [context beginBatchFetching];
    
}
// 3
- (void)tableNode:(ASTableNode *)tableNode didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableNode deselectRowAtIndexPath:indexPath animated:YES];
    

}
- (void)insertNewRowsInTableNode:(NSArray<AsynModel *>*)array
{
    NSInteger section = 0;
    NSMutableArray *indexPaths = [NSMutableArray array];
    
    for (NSUInteger row = _dataArr.count-array.count; row < _dataArr.count; row++) {
        NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];
        [indexPaths addObject:path];
    }
    [_tableNode insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}


- (NSMutableArray *)dataArr{
    if (_dataArr == nil) {
        AsynModel *model = [[AsynModel alloc]init];
        AsynModel *model1 = [[AsynModel alloc]init];
        AsynModel *model2 = [[AsynModel alloc]init];
        model.title = @"11111";
        model1.title = @"22222";
        model2.title = @"33333";
        _dataArr = [[NSMutableArray alloc]initWithArray:@[model,model1,model2]];
    }
    return _dataArr;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
